package com.tempo.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by tempo on 2015/11/16.
 */
public class SectionBar extends FrameLayout {
    private ImageView ivLeft;
    private TextView tvTitle;
    private View btnSectionBar;
    private OnClickListener mOnclickListener;
    private TypedArray ta;

    public SectionBar(Context context) {
        this(context,null);
    }

    public SectionBar(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SectionBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        ta = context.obtainStyledAttributes(attrs,R.styleable.SectionBar);
        LayoutInflater.from(context).inflate(R.layout.sectionbar_layout, this, true);
        init();
    }

    private void init() {
        ivLeft = (ImageView) findViewById(R.id.iv_left);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        btnSectionBar = findViewById(R.id.btn_sectionbar);
        String title= ta.getString(R.styleable.SectionBar_sectionbar_title);
        tvTitle.setText(title);
        int  imgId=ta.getResourceId(R.styleable.SectionBar_sectionbar_titleImg,-1);
        if(imgId!=-1){
            ivLeft.setImageResource(imgId);
        }
        btnSectionBar.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnclickListener!=null)
                    mOnclickListener.onClick(btnSectionBar);
            }
        });
    }

    public void setTitle(String title){
        tvTitle.setText(title);
    }
    public void setTitleImageResource(int id){
        ivLeft.setImageResource(id);
    }
    public void setOnSectionBarClickListtener(OnClickListener onClickListener){
        mOnclickListener=onClickListener;
    }
}
